package com.amadeus.spin.standard.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLEntityDecoder {
  private String entityCharacters[] = { "&", "'", "<", ">", "\"" };

  private String entityNames[] = { "&amp;", "&apos;", "&lt;", "&gt;", "&quot;" };

  public HTMLEntityDecoder() {

  }

  /**
   * Converts some html entities (&amp; &apos; &lt; &gt; &quot;) into their
   * corresponding character (& ' < > ").
   * 
   * @param inputString
   *          The string to decode.
   * @return The decoded string.
   */
  public String decode(String inputString) {
    String replaceStr = null;
    for (int x = 0; x < entityCharacters.length; x++) {
      String patternStr = "(" + entityNames[x] + ")";
      replaceStr = entityCharacters[x];
      Pattern pattern = Pattern.compile(patternStr);
      Matcher matcher = pattern.matcher(inputString);
      inputString = matcher.replaceAll(replaceStr);
    }
    return inputString;
  }

}
